﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// AmbientLightText.xaml 的交互逻辑
    /// </summary>
    public partial class AmbientLightText : UserControl
    {
        public AmbientLightText()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(AmbientLightText), new PropertyMetadata(""));


        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(AmbientLightText), new PropertyMetadata(""));


        public Brush ValueColor
        {
            get { return (Brush)GetValue(ValueColorProperty); }
            set { SetValue(ValueColorProperty, value); }
        }

        public static readonly DependencyProperty ValueColorProperty =
            DependencyProperty.Register("ValueColor", typeof(Brush), typeof(AmbientLightText), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#91DBDB"))));


        public Color RadialColor
        {
            get { return (Color)GetValue(RadialColorProperty); }
            set { SetValue(RadialColorProperty, value); }
        }

        public static readonly DependencyProperty RadialColorProperty =
            DependencyProperty.Register("RadialColor", typeof(Color), typeof(AmbientLightText), new PropertyMetadata((Color)ColorConverter.ConvertFromString("#1FDCF5")));

        public Color LinearColor
        {
            get { return (Color)GetValue(LinearColorProperty); }
            set { SetValue(LinearColorProperty, value); }
        }

        public static readonly DependencyProperty LinearColorProperty =
            DependencyProperty.Register("LinearColor", typeof(Color), typeof(AmbientLightText), new PropertyMetadata((Color)ColorConverter.ConvertFromString("#331FDCF5")));

        public AmbientState State
        {
            get => (AmbientState)GetValue(StateProperty);
            set => SetValue(StateProperty, value);
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register(nameof(State), typeof(AmbientState), typeof(AmbientLightText),
                new PropertyMetadata(AmbientState.Normal, OnStateChanged));

        private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AmbientState newValue = (AmbientState)e.NewValue;
            var control = (AmbientLightText)d;
            switch (control.State)
            {
                case AmbientState.Normal:
                    Color normalRg = (Color)control.TryFindResource("RadialGradientBrush.Normal");
                    Color normalLn = (Color)control.TryFindResource("LinearGradientBrush.Normal");
                    control.SetCurrentValue(RadialColorProperty, normalRg);
                    control.SetCurrentValue(LinearColorProperty, normalLn);
                    break;
                case AmbientState.Warn:
                    Color warnRg = (Color)control.TryFindResource("RadialGradientBrush.Warn");
                    Color warnLn = (Color)control.TryFindResource("LinearGradientBrush.Warn");
                    control.SetCurrentValue(RadialColorProperty, warnRg);
                    control.SetCurrentValue(LinearColorProperty, warnLn);
                    break;
                case AmbientState.Error:
                    Color errorRg = (Color)control.TryFindResource("RadialGradientBrush.Error");
                    Color errorLn = (Color)control.TryFindResource("LinearGradientBrush.Error");
                    control.SetCurrentValue(RadialColorProperty, errorRg);
                    control.SetCurrentValue(LinearColorProperty, errorLn);
                    break;
                default:
                    throw new Exception($"未知的状态类型: {control.State}");
                    break;
            }
        }
    }
    public enum AmbientState
    {
        Normal,
        Warn,
        Error
    }
}
